//
//  FoodListView.m
//  ArtOfLiving
//
//  Created by xalo on 16/5/1.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "FoodListView.h"
#import "FoodNutritionDataModel.h"
#import "FoodComponentModel.h"

@implementation FoodListView

- (instancetype)initWithListFrame:(CGRect)frame listArray:(NSArray *)listArray {
    
    frame.size.height = 30 * listArray.count;
    frame.size.width = kScreenWidth;
    self.frame = frame;
    self = [super initWithFrame:frame];
    if (self) {
        
        for (int i = 0; i < listArray.count; i++) {
            
            UIView *listCell = [self listCellWithModel:listArray[i] index:i];
            [self addSubview:listCell];
        }
    }
    return self;
}

- (UIView *)listCellWithModel:(id)model index:(int)index {
    
    UIView *listCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 30*index, kScreenWidth, 30)];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth/3.0, 30.0)];
    UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/3.0, 0.0, kScreenWidth/3.0, 30.0)];
    UILabel *unitlLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/3.0*2.0, 0.0, kScreenWidth/3.0, 30.0)];
    if ([model isKindOfClass:[FoodNutritionDataModel class]]) {
        
        FoodNutritionDataModel *nutritionModel = model;
        nameLabel.text = nutritionModel.name;
        amountLabel.text = [NSString stringWithFormat:@"%0.2f", [nutritionModel.amount floatValue]];
        unitlLabel.text = nutritionModel.unit;
    } else if ([model isKindOfClass:[FoodComponentModel class]]) {
        
        FoodComponentModel *componentModel = model;
        nameLabel.text = componentModel.ingredientDic.name;
        amountLabel.text = [NSString stringWithFormat:@"%0.2f", [componentModel.amount floatValue]];
        unitlLabel.text = componentModel.remark;
    }
    [listCellView addSubview:nameLabel];
    [listCellView addSubview:amountLabel];
    [listCellView addSubview:unitlLabel];
    return listCellView;
}


@end
